package 牛客剑指专题二刷.链表;

/**
 * author:JiangSong
 * Date:2023/7/3
 **/

/**
 * 合并两个排序的链表
 法1：尾插
 */

public class JZ25 {
    class ListNode{
        int val;
        ListNode next = null;
        ListNode(int val){
            this.val = val;
        }
    }

    public ListNode Merge (ListNode pHead1, ListNode pHead2) {
        if(pHead1 == null)return pHead2;
        if(pHead2 == null)return pHead1;
        ListNode head =new ListNode(0);
        ListNode cur = head;
        while(pHead1 != null && pHead2 != null){
            if(pHead1.val <= pHead2.val){
                cur.next = pHead1;
                pHead1 = pHead1.next;
            }else{
                cur.next = pHead2;
                pHead2 = pHead2.next;
            }
            cur = cur.next;
        }
        cur.next = pHead1==null ? pHead2 : pHead1;
        return head.next;
    }

}
